<template>
    <div class="views-dianyingxinxi-add">
        <div>
            <el-card class="box-card">
                <template #header>
                    <div class="clearfix">
                        <span class="title"> 添加电影信息 </span>
                    </div>
                </template>

                <el-form :model="form" ref="formModel" label-width="140px" status-icon validate-on-rule-change>
                    <el-form-item label="电影编号" prop="dianyingbianhao" :rules="[{required:true, message:'请填写电影编号'}]">
                        <el-input type="text" placeholder="输入电影编号" style="width: 450px" v-model="form.dianyingbianhao" />
                    </el-form-item>

                    <el-form-item label="电影名称" prop="dianyingmingcheng" required :rules="[{required:true, message:'请填写电影名称'}]">
                        <el-input type="text" placeholder="输入电影名称" style="width: 450px" v-model="form.dianyingmingcheng" />
                    </el-form-item>

                    <el-form-item label="分类" prop="fenlei" required :rules="[{required:true, message:'请填写分类'}]">
                        <el-select v-model="form.fenlei"><e-select-option type="option" module="dianyingfenlei" value="id" label="fenleimingcheng"></e-select-option></el-select>
                    </el-form-item>

                    <el-form-item label="电影类型" prop="dianyingleixing" required :rules="[{required:true, message:'请填写电影类型'}]">
                        <el-select v-model="form.dianyingleixing"
                            ><el-option label="已上映" value="已上映"></el-option>
                            <el-option label="即将上映" value="即将上映"></el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item
                        label="影票价格"
                        prop="yingpiaojiage"
                        required
                        :rules="[{required:true, message:'请填写影票价格'}, {validator:rule.checkNumber, message:'输入一个有效数字'}]"
                    >
                        <el-input type="number" placeholder="输入影票价格" style="width: 450px" v-model.number="form.yingpiaojiage" />
                    </el-form-item>

                    <el-form-item label="电影海报" prop="dianyinghaibao"> <e-upload-images v-model="form.dianyinghaibao" is-paste></e-upload-images> </el-form-item>

                    <el-form-item label="上映地区" prop="shangyingdiqu" required :rules="[{required:true, message:'请填写上映地区'}]">
                        <el-input type="text" placeholder="输入上映地区" style="width: 450px" v-model="form.shangyingdiqu" />
                    </el-form-item>

                    <el-form-item label="导演" prop="daoyan" required :rules="[{required:true, message:'请填写导演'}]">
                        <el-input type="text" placeholder="输入导演" style="width: 450px" v-model="form.daoyan" />
                    </el-form-item>

                    <el-form-item label="主演" prop="zhuyan" required :rules="[{required:true, message:'请填写主演'}]">
                        <el-input type="text" placeholder="输入主演" style="width: 450px" v-model="form.zhuyan" />
                    </el-form-item>

                    <el-form-item label="语言" prop="yuyan" required :rules="[{required:true, message:'请填写语言'}]">
                        <el-input type="text" placeholder="输入语言" style="width: 450px" v-model="form.yuyan" />
                    </el-form-item>

                    <el-form-item label="年份" prop="nianfen" required :rules="[{required:true, message:'请填写年份'}]">
                        <el-input type="text" placeholder="输入年份" style="width: 450px" v-model="form.nianfen" />
                    </el-form-item>

                    <el-form-item label="片长" prop="pianchang" required :rules="[{required:true, message:'请填写片长'}]">
                        <el-input type="text" placeholder="输入片长" style="width: 450px" v-model="form.pianchang" />
                    </el-form-item>

                    <el-form-item label="是否上架" prop="shifoushangjia" required :rules="[{required:true, message:'请填写是否上架'}]">
                        <el-select v-model="form.shifoushangjia"
                            ><el-option label="上架" value="上架"></el-option>
                            <el-option label="下架" value="下架"></el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="添加人" prop="tianjiaren"> <el-input v-model="form.tianjiaren" readonly style="width: 250px"></el-input> </el-form-item>

                    <el-form-item label="电影详情" prop="dianyingxiangqing">
                        <e-editor v-model="form.dianyingxiangqing" @getContent="getdianyingxiangqingContent"></e-editor>
                    </el-form-item>

                    <el-form-item v-if="btnText">
                        <el-button type="primary" @click="submit">{{ btnText }}</el-button>
                    </el-form-item>
                </el-form></el-card
            >
        </div>
    </div>
</template>

<script setup>
    import http from "@/utils/ajax/http";
    import DB from "@/utils/db";
    import rule from "@/utils/rule";
    import router from "@/router";
    import EEditor from "@/components/EEditor.vue";

    import { ref, reactive } from "vue";
    import { useRoute } from "vue-router";
    import { session } from "@/utils/utils";
    import { ElMessage, ElMessageBox } from "element-plus";
    import { useDianyingxinxiCreateForm, canDianyingxinxiInsert } from "@/module";
    import { extend } from "vue-design-plugin";

    const route = useRoute();
    const props = defineProps({
        btnText: {
            type: String,
            default: "保存",
        },
        isRead: {
            type: Boolean,
            default: true,
        },
        isHouxu: {
            type: Boolean,
            default: true,
        },
    });
    const { form } = useDianyingxinxiCreateForm();
    const emit = defineEmits(["success"]);
    const formModel = ref();
    const loading = ref(false);
    var submit = () => {
        formModel.value.validate().then((res) => {
            if (loading.value) return;
            loading.value = true;
            canDianyingxinxiInsert(form).then(
                (res) => {
                    loading.value = false;
                    if (res.code == 0) {
                        emit("success", res.data);
                        if (props.isHouxu) {
                            ElMessage.success("添加成功");
                            const f = useDianyingxinxiCreateForm();
                            extend(form, f.form);
                        }
                    } else {
                        ElMessageBox.alert(res.msg);
                    }
                },
                (err) => {
                    loading.value = false;
                    ElMessageBox.alert(err.message);
                }
            );
        });
    };

    const getdianyingxiangqingContent = (v) => {
        form.dianyingxiangqing = v;
    };
</script>

<style scoped lang="scss">
    .views-dianyingxinxi-add {
    }
</style>
